package com.fuqi.android.zhongcai.sql.generator;

/**
 * Created by juner on 2014/8/3.
 * 数据库表和操作的生成
 */


import de.greenrobot.daogenerator.DaoGenerator;
import de.greenrobot.daogenerator.Entity;
import de.greenrobot.daogenerator.Property;
import de.greenrobot.daogenerator.Schema;
import de.greenrobot.daogenerator.ToMany;

public class TableDaoGenerator {
    public static void main(String[] args) throws Exception {
        Schema schema = new Schema(4, "com.fuqi.android.zhongcai.sql.temp");

        addUser(schema);
        addWorkerTable(schema);
        addDic(schema);
        addCacheImg(schema);
        new DaoGenerator().generateAll(schema, "./app/src/main/java");
    }

    /**
     * uuser table.

     +---------+--------------+------+-----+---------+----------------+
     | Field   | Type         | Null | Key | Default | Extra          |
     +---------+--------------+------+-----+---------+----------------+
     | birthday| varchar(50)  | NO   |     | NULL    |                |
     | phone   | mediumtext   | NO   |     | NULL    |                |
     | sex     | varchar(50)  | NO   |     | NULL    |                |
     | userid  | varchar(100) | NO   |     | NULL    |                |
     | rolecode| varchar(50)  | NO   |     | NULL    |                |
     | name    | varchar(100) | NO   |     | NULL    |                |
     | token   | varchar(100) | NO   |     | NULL    |                |
     +---------+--------------+------+-----+---------+----------------+

     */
    private static void addUser(Schema schema) {

        Entity user = schema.addEntity("User");
        user.addIntProperty("id").notNull();
        user.addStringProperty("userid").notNull();
        user.addStringProperty("phone");
        user.addStringProperty("name");
        user.addStringProperty("sex");
        user.addStringProperty("rolecode");
        user.addStringProperty("birthday");
        user.addStringProperty("token");
        user.addStringProperty("type");
    }

    /**
     * customer table.

     +---------+--------------+------+-----+---------+----------------+
     | Field   | Type         | Null | Key | Default | Extra          |
     +---------+--------------+------+-----+---------+----------------+
     | id      | int(11)      | NO   | PRI | NULL    | auto_increment |
     | name    | varchar(200) | NO   |     | NULL    |                |
     | age     | int(11)      | YES  |     | NULL    |                |
     +---------+--------------+------+-----+---------+----------------+

     */

    /**
     * ORDERS table.

     +--------------+--------------+------+-----+---------+----------------+
     | Field        | Type         | Null | Key | Default | Extra          |
     +--------------+---------+----+-+----++----+----+----+----------------+
     | id           | int(11)      | NO   | PRI | NULL    | auto_increment |
     | customerId   | varchar(200) | NO   |     | NULL    |                |
     | content      | mediumtext   | NO   |     | NULL    |                |
     +--------------+--------------+------+-----+---------+----------------+

     */
    private static void addCustomerOrder(Schema schema) {
        Entity customer = schema.addEntity("Customer");
        customer.addIdProperty();
        customer.addStringProperty("name").notNull();
        customer.addIntProperty("age");

        Entity order = schema.addEntity("Order");
        order.setTableName("ORDERS"); // "ORDER" is a reserved keyword
        order.addIdProperty();
        Property orderDate = order.addDateProperty("date").getProperty();
        Property customerId = order.addLongProperty("customerId").notNull().getProperty();
        order.addToOne(customer, customerId);

        ToMany customerToOrders = customer.addToMany(order, customerId);
        customerToOrders.setName("orders");
        customerToOrders.orderAsc(orderDate);
    }

    /**
     * Worker table.
     +---------+--------------+------+-----+---------+----------------+
     | Field   | Type         | Null | Key | Default | Extra          |
     +---------+--------------+------+-----+---------+----------------+
     | name    | varchar(200) | NO   |     | NULL    |                |
     | pinyin  | varchar(200) | NO   |     | NULL    |                |
     +---------+--------------+------+-----+---------+----------------+

     */

    private static void addWorkerTable(Schema schema) {
        Entity scenic = schema.addEntity("Worker");
        scenic.addStringProperty("name").notNull().unique();
        scenic.addStringProperty("pinyin").notNull();
    }

    private static void addDic(Schema schema) {
        Entity scenic = schema.addEntity("Dic");
        scenic.addStringProperty("id").notNull().unique();
        scenic.addStringProperty("name").notNull();
        scenic.addStringProperty("bianma").notNull();
        scenic.addStringProperty("order");
        scenic.addStringProperty("pbianma");
    }

    private static void addCacheImg(Schema schema) {
        Entity scenic = schema.addEntity("CacheImg");
        scenic.addStringProperty("id").notNull();
        scenic.addStringProperty("ordid").notNull();
        scenic.addStringProperty("path").notNull();
        scenic.addStringProperty("ordtype").notNull();
    }
}

